//
//  CityViewController.m
//  PickerView
//
//  Created by cocoazxj@163.com on 15/4/1.
//  Copyright (c) 2015年 handcool. All rights reserved.
//

#import "CityViewController.h"
#import "CityModel.h"

@interface CityViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) IBOutlet UIPickerView *pv_view;
@property (nonatomic, strong) NSArray *mArray;
@end

@implementation CityViewController

- (NSArray *)mArray
{
    if (!_mArray)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"cities" ofType:@".plist"];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *m = [NSMutableArray array];
        for(NSDictionary *d in array)
        {
            [m addObject:[CityModel cityModel:d]];
        }
        _mArray = m;
    }
    return _mArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    for(CityModel *m in self.mArray)
    {
        NSLog(@"-----%@",m.province);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0)
    {
        return self.mArray.count;
    }
    else
    {
//        int index = [pickerView selectedRowInComponent:0];
        CityModel *m = self.mArray[[pickerView selectedRowInComponent:0]];
        
        return m.city.count;
    }
    return 2;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0)
    {
        CityModel *m = self.mArray[row];
        return m.province;
    }
    else
    {
//        int index = [pickerView selectedRowInComponent:0];
        CityModel *m = self.mArray[[pickerView selectedRowInComponent:0]];
        return m.city[row];
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(component == 0)
    {
        [pickerView reloadComponent:1];
        
    }
}
@end
